package com.example.service.webhook.business.carrier.repository;

import com.example.service.webhook.business.carrier.entity.MobileSmsEntity;
import com.example.service.webhook.business.carrier.entity.MobileVoiceCallEntity;
import com.example.service.webhook.business.carrier.entity.NetFlowEntity;
import com.example.service.webhook.repository.base.BaseDAO;
import com.example.service.webhook.util.DateUtil;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ParameterizedPreparedStatementSetter;
import org.springframework.stereotype.Repository;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

/**
 * 运营商套餐适用情况
 * ClassName: PackageUsageRepository
 * date: 2016年7月19日 下午6:29:23   
 * @author yuandong  
 * @version   
 * @since JDK 1.6
 */
@Repository
public class MobileNetRepository extends BaseDAO<NetFlowEntity>{
	
	@Resource(name="templateCarrier")
    private JdbcTemplate jdbcTemplate;

    public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}
    
    public void saveNetFlow(NetFlowEntity entity) throws Exception  {
    	entity.setCreateTime(DateUtil.getCurrentDate());
    	entity.setLastModifyTime(DateUtil.getCurrentDate());
		this.add(entity);
    }
    
    public int[][] batchSave(String userId, List<NetFlowEntity> netflows) {
		
		int[][] updateCounts = jdbcTemplate.batchUpdate(
				"insert into t_netflow (`userId`,`mobile`,`billMonth`,`time`,`location`,`serviceName`,`netType`,`durationInSecond`,`durationInFlow`,`fee`,`createTime`,`lastModifyTime`) values"
						+ " (?,?,?,?,?,?,?,?,?,?,?,?)",
						netflows,
				2000,
				new ParameterizedPreparedStatementSetter<NetFlowEntity>() {
					public void setValues(PreparedStatement ps, NetFlowEntity entity) throws SQLException {
						ps.setString(1, entity.getUserId());
						ps.setString(2, entity.getMobile());
						ps.setString(3,entity.getBillMonth());
						ps.setString(4, entity.getTime());
						ps.setString(5, entity.getLocation());
						ps.setString(6, entity.getServiceName());
						ps.setString(7, entity.getNetType());
						ps.setInt(8, entity.getDurationInSecond()==null?0:entity.getDurationInSecond());
						ps.setInt(9, entity.getDurationInFlow()==null?0:entity.getDurationInFlow());
						ps.setInt(10, entity.getFee()==null?0:entity.getFee());
						ps.setDate(11, new Date(DateUtil.getCurrentDate().getTime()));
						ps.setDate(12, new Date(DateUtil.getCurrentDate().getTime()));
					}
				});
		return updateCounts;
	}
    
    public void deleteMobileNetFlow(String userId,String mobile,String billMonth){
    	String sql = "delete from t_netflow where userId=? and mobile=? and billMonth=?";
    	this.delete(sql, new Object[]{userId,mobile,billMonth});
    }
    
    
    
}
